/*
 * @Description: 妆容收藏
 * @Author: kanglin
 * @Date: 2018-06-13 10:38:23
 * @Copyright: Created by Panxsoft.
 */

import API_LIST from 'src/API_LIST';
import axios from 'axios';

const state = {
	collection_detail: null,
};

const mutations = {
	/**
     * 保存妆容详情
     * @param {*} state
     * @param {*} detail
     */
	saveCollectionDetail(state, detail) {
		state.collection_detail = detail;
	},
};

const getters = {
	/**
     * 读取妆容详情
     * @param {*} state
     */
	getCollectionDetail(state) {
		return state.collection_detail;
	},
};

const actions = {
	/**
	 *  添加收藏
	 * @return {promise} result
	 */
	postCollectionAction(context, { id, form }) {
		const body = { ...form, _method: 'put' };
		return axios.post(`${API_LIST.USER_FAVORS}/${id}`, body)
			.then(({ data }) => {
				if (data.code !== 0) {
					throw new Error(data.error);
				}
				return data;
			});
	},

	/**
	 * 请求收藏列表
	 * @param {*} context
	 * @param {*} params
	 */
	fetchCollectionIndexAction(context, params) {
		return axios.get(API_LIST.USER_FAVORS, { params })
			.then(({ data }) => {
				if (data.code !== 0) {
					throw new Error(data.error);
				} else {
					return data;
				}
			});
	},

	/**
	 * 请求收藏详情
	 * @param {number|string} id
	 */
	fetchCollectionAction(context, id) {
		return axios.get(`${API_LIST.USER_FAVORS}/${id}`)
			.then(({ data }) => {
				if (data.code !== 0) {
					throw new Error(data.error);
				} else {
					context.commit('saveCollectionDetail', data.favor);
					return data;
				}
			});
	},

};


export default {
	namespaced: true,
	getters,
	mutations,
	state,
	actions,
};

